package com.yx.xinruitu.util;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @program: xinruitu
 * @description
 * @author: WangYouBin
 * @create: 2019-12-18 11:23
 **/
public class ExprotCellStyle {


    /**
     * 得到表头样式
     *
     * @param workbook
     * @return
     */
    public static XSSFCellStyle createTableTitleStyle(XSSFWorkbook workbook) {
        //水平居中
        XSSFCellStyle cellStyle = setRowCellCenter(workbook);
        //设置字体
        XSSFFont font = setFontCellStyle(workbook, (short) 11, false);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        return cellStyle;
    }

    /**
     * 创建小标题的样式
     *
     * @param workbook
     * @return
     */
    public static XSSFCellStyle createSecondTitleStyle(XSSFWorkbook workbook) {
        //水平居中
        XSSFCellStyle cellStyle = setRowCellCenter(workbook);
        //设置字体
        XSSFFont font = setFontCellStyle(workbook, (short) 11, false);
        cellStyle.setFont(font);
        return cellStyle;
    }

    /**
     * 创建表头的样式
     *
     * @param workbook
     * @return
     */
    public static XSSFCellStyle createTitleCellStyle(XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = setRowCellCenter(workbook);
        //设置字体
        XSSFFont font = setFontCellStyle(workbook, (short) 11, false);
        cellStyle.setFont(font);
        return cellStyle;
    }

    /**
     * @param workbook 工作簿
     * @param fontSize 字体大小
     * @param italic   是否斜体
     * @return
     */
    public static XSSFFont setFontCellStyle(XSSFWorkbook workbook,
                                            short fontSize, boolean italic
    ) {
        XSSFFont font = workbook.createFont();
        //字体大小
        font.setFontHeightInPoints(fontSize);
        //设置非斜体
        font.setItalic(italic);
        return font;
    }

    /**
     * 创建水平和垂直居中的方法
     *
     * @param workbook
     * @return
     */
    public static XSSFCellStyle setRowCellCenter(XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        //水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        setCell(cellStyle);
        return cellStyle;
    }

    public static XSSFCellStyle setCell(XSSFCellStyle cellStyle) {
        //下边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        //上边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        //左边框;
        cellStyle.setBorderLeft(BorderStyle.THIN);
        //右边框;
        cellStyle.setBorderRight(BorderStyle.THIN);
        return cellStyle;
    }


}
